<?php 
class agentClassAction extends ActionManage
{
	public function defaultAction()
	{
		
	}
	
	public function dataAction()
	{
		$limit= (int)$this->get('limit','1');
		$sort = $this->get('sort');
		$dir  = $this->get('dir');
		$order= '`id` desc';
		if($sort && $dir)$order= '`'.$sort.'` '.$dir.'';
		$dbs 	= m('agent');
		$rows   = $dbs->getall('`cid`='.$this->companyid.'','*', '`sort` asc');
		foreach($rows as $k=>$rs)$rows[$k]['picurl'] = getface($rs['picurl'], $dbs->defpic);
		
		$total  = count($rows);
		
		
		
		$barr = array(
			'rows' 		=> $rows,
			'totalCount'=> count($rows),
			'success' 	=> true,
		);
		return $barr;
	}
	
	public function formAction()
	{	
		$id = (int)$this->get('id','0');
		$this->smartydata['id'] = $id;
			
		$fields = c('fields')->getArr('agent');
		$rs 	= $fields['data'];
		$fobj 	= $fields['fieldsobj'];
		$dbs 	= m('agent');
		if($id>0){
			$rs 	= $dbs->getone('`id`='.$id.'');
			if(!isempt($rs['picurl']))$rs['picurlpic'] = getface($rs['picurl'], $dbs->defpic);
		}
		if(isempt(arrvalue($rs,'picurlpic')))$rs['picurlpic'] = $dbs->defpic;

		$this->assign('fieldsobj', $fobj);
		$this->assign('data', $rs);
	}
	
	public function savecheckAction()
	{
		
		$id = (int)$this->post('id','0');
		$num= $this->post('num');
		$to = m('agent')->rows('`cid`='.$this->companyid.' and `num`=\''.$num.'\' and `id`<>'.$id.'');
		if($to>0)return ''.$num.'编号已存在';
		
		$uarr= array(
			'name' 		=> $this->post('name'),
			'cid' 		=> $this->companyid,
			'num' 		=> $num,
			'url' 		=> $this->post('url'),
			'murl' 		=> $this->post('murl'),
			'picurl' 	=> $this->post('picurl'),
			'recename' 	=> $this->post('recename'),
			'receid' 	=> $this->post('receid'),
			'types' 	=> $this->post('types'),
			'fzid' 		=> $this->post('fzid'),
			'fzname' 	=> $this->post('fzname'),
			'sort' 		=> (int)$this->post('sort','0'),
			'status' 	=> (int)$this->post('status','0'),
			'optdt' 	=> $this->rock->now
		);
		$where = '`id`='.$id.' and `cid`='.$this->companyid.'';
		if($id==0){
			$where = '';
			$uarr['agentkey'] = $this->db->ranknum('[Q]agent','agentkey',10);
		}
		$bo 	= m('agent')->record($uarr, $where);
		if(!$bo)return $this->db->error();
		
		return 'ok';
	}
	
	
	public function sendmsgAction()
	{
		$agentid = (int)$this->post('agentid','0');
		$cont 	 = $this->post('cont');
		return m('agent')->sendmsg('u'.$this->useraid.'', $agentid, $cont);
	}
}